/**
 * jims
 */
package com.jims.master.modules.oper.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.entity.LabType;
import com.jims.master.modules.oper.service.ClinicItemDictService;
import com.jims.master.modules.oper.service.LabTypeService;
import com.jims.master.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.oper.entity.LabVsClinic;
import com.jims.master.modules.oper.service.LabVsClinicService;

import java.util.ArrayList;
import java.util.List;

/**
 * 检验项目管理Controller
 * @author CT
 * @version 2017-02-27
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/labVsClinic")
public class LabVsClinicController extends BaseController {

	@Autowired
	private ClinicItemDictService clinicItemDictService;
	@Autowired
	private LabVsClinicService labVsClinicService;
	@Autowired
	private LabTypeService labTypeService;
	
	@ModelAttribute
	public LabVsClinic get(@RequestParam(required=false) String id) {
		LabVsClinic entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = labVsClinicService.get(id);
		}
		if (entity == null){
			entity = new LabVsClinic();
		}
		return entity;
	}

	/**
	 * 加载检验类别列表
	 * @param labVsClinic
	 * @param request
	 * @param response
	 * @param model
	 * @return 陈涛
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"labVsClinicList", ""})
	public String list(String labTypeId, LabVsClinic labVsClinic, HttpServletRequest request, HttpServletResponse response, Model model) {
        if(labTypeId!=null && !"undefined".equals(labTypeId)){
			labVsClinic.setLabId(labTypeId);
		}
        List<LabVsClinic> list = labVsClinicService.findList(labVsClinic);
		model.addAttribute("list", list);
        model.addAttribute("labTypeId", labTypeId);
		return "modules/oper/labVsClinicIndex";
	}

	/**
	 * 加载检验类别页面（右）
	 * @param labTypeId
	 * @param labVsClinic
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"labItemsRight", ""})
    public String labItemsRight(String labTypeId,LabVsClinic labVsClinic ,Model model){
        List<LabVsClinic> list=new ArrayList<>();
        if(labTypeId!=null && !"undefined".equals(labTypeId)) {
            labVsClinic.setLabId(labTypeId);
            list = labVsClinicService.findList(labVsClinic);
        }
		model.addAttribute("list", list);
     return "modules/oper/labVsClinicRight";
	}
	/**
	 * 跳到检验类别新增页面
	 * @param labVsClinic
	 * @param model
	 * @return  陈涛
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "labVsClinicForm")
	public String form(LabVsClinic labVsClinic, Model model) {
		String labId = labVsClinic.getLabId();
        LabType  labType =labTypeService.get(labId);
		labVsClinic.setOrgDept(labType.getOrgDept());
		labVsClinic.setLabType(labType);
		model.addAttribute("labVsClinic", labVsClinic);
		model.addAttribute("labType",labType);
		return "modules/oper/labVsClinicForm";
	}

	/**
	 * 保存 修改 信息
	 * @param labVsClinic
	 * @param model
	 * @return  陈涛
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveLabItem")
	@ResponseBody
	public StringData save(LabVsClinic labVsClinic, Model model) {
	    StringData data=new StringData();
		int i=labVsClinicService.save(labVsClinic);
        if(i>0){
            data.setCode("success");
            data.setData("保存检验类别成功");
        }else{
            data.setCode("error");
            data.setData("保存检验类别失败");
        }
        return data;
	}

	/**
	 * 数据删除
	 * @param labVsClinic
	 * @return  陈涛
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "deleteLabItem")
	@ResponseBody
	public StringData delete(LabVsClinic labVsClinic) {
	    StringData data=new StringData();
		int i=labVsClinicService.delete(labVsClinic);
        if(i>0){
            data.setCode("success");
            data.setData("删除检验类别成功");
        }else{
            data.setCode("error");
            data.setData("删除检验类别失败");
        }
		return data;
	}



}